
#include <boost/xpressive/xpressive_dynamic.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>

#include "Config.h"

#include "Common.h"

//using namespace boost;

namespace RisingStar
{
	bool Common::FindFiles(const path& dir, const string& filenameExt,vector<path>& v)
	{

		if(!exists(dir) || !is_directory(dir))
			return false;
		rd_iterator end;
		for(rd_iterator pos(dir);pos != end;++pos)
		{
			if(!is_directory(*pos))
			{
				string ext = pos->path().extension().string();
				if(ext == filenameExt)
				v.push_back(pos->path());
				
			}
			
		}
		return true;
	}
	string Common::GetLocalTime()
	{
		char tbuf[255] = {0};
		struct tm *t;
		time_t tt;
		time(&tt);
		t=localtime(&tt);
		
		sprintf(tbuf,"%4d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
		return tbuf;

	}
	 int Common::LOG(const char* fun, int line,string event,string logFile,string label)
	 {

		/**/char tbuf[255] = {0};

		struct tm *t;
		time_t tt;
		time(&tt);
		t=localtime(&tt);
		
		sprintf(tbuf,"%4d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
		
		string localTime = GetLocalTime();
		if(label == "")
		{
			label = YSTR("事件");
		}
		cout<<label<<"["<<localTime<<"]:"<<event<<endl;
		if(logFile != "")
		{
			char buf[513] = {0};
			char pbuf[65] = {0};
			char fbuf[65] = {0};
			sprintf(fbuf,"%4d-%02d-%02d.log",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
			sprintf(pbuf,"mkdir -p /mnt/mmcblk0p1/mpHistory/log/%04d",t->tm_year+1900);
			system(pbuf);
			
			snprintf(buf,512,"echo \"time:%s fun:%s,line:%d, event:%s\" >>  /mnt/mmcblk0p1/mpHistory/log/%04d/%s ",
						localTime.c_str(),
						fun,
						line,
						event.c_str(),
						t->tm_year+1900,
						fbuf
						);
			
			std::system(buf);
		}
		return 0;
	 }
	 
	string Common::FixString(string in,int width,char fix)
	{
		stringstream ss;
		ss.width(width);
		ss.fill(fix);
		ss<<in;
		string ret;
		ss>>ret;
		return ret;
	}
	string Common::FixString(int in,int width,char fix)
	{
		stringstream ss;
		ss.width(width);
		ss.fill(fix);
		ss<<in;
		string ret;
		ss>>ret;
		return ret;
	}
	bool Common::ExtractVariableFromFormula(string& formula,string& val)
	{
		boost::regex oRegex("(?<={{)([\u4e00-\u9fa5_a-zA-Z0-9]+)(?=\\W)");
		boost::smatch oResults;   
		 if(boost::regex_search(formula, oResults, oRegex))   
        {   
            //std::cout << formula << " matches " << oRegex << "\n";   
			if (oResults.size() >0)
			{
				if (oResults[0].matched)
				{
					//cout<<"formula:"<<formula<<"  match:"<<oResults[0]<<endl;
					val = oResults[0];
					return true;
				}
					//std::cout << oResults[i] << std::endl;
			}
        }
		return false;
	}
	
	string Common::ReplaceString(string str,string oldKeyStr,string newKeyStr)
	{
		string rst;
		int i = 0;
		int j = 0;
		const char* oldKey = oldKeyStr.c_str();
		int oldKeySize = oldKeyStr.length(); 
		const char* s = str.c_str();
		int strSize = str.length();

		for(i = 0; i < strSize; i++)
		{
			for(j = 0; j < oldKeySize && i + j < strSize; j++)
			{
				if(s[i + j] != oldKey[j])
					break;
			}
			if(j == oldKeySize)	
			{
				rst +=  newKeyStr;
				i += j;
			}
			rst += s[i];

		}
		
		return rst;
	}
	
	//
}